/*java jdk 1.7+
# -*- coding: utf-8 -
'''
Created on 2020年05月10日
@author: zxyong 13738196011
'''*/

package com.zxy.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.Date;

public class Auto_Command
{
	public String	strAInput	= "";
	public String	strType		= "";
	public String	strResult	= "";

	public void run()
	{
		if(strType.equals("useLinuxComdParam"))
			useLinuxComdParam(strAInput);
		else
			useLinuxCommond(strAInput);
	}

	public void useLinuxNoResult(String strInput)
	{
		InputStreamReader ir = null;
		BufferedReader input = null;
		try
		{
			Process p = Runtime.getRuntime().exec(strInput); // 调用Linux的相关命令
			ir = new InputStreamReader(p.getInputStream());
			input = new BufferedReader(ir);
			String line = "";
			while(!java.util.Objects.equals((line = input.readLine()),null))
			{
				strResult += line.trim() + "\r\n";
				// 按行打印输出内容
			}
		}
		catch(IOException e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		finally
		{
			try
			{
				ir.close();
			}
			catch(IOException e)
			{
			}
			try
			{
				input.close();
			}
			catch(IOException e)
			{
			}
		}
	}

	public String useLinuxComdParam(String strInput)
	{
		String strResult = "";
		InputStreamReader ir = null;
		BufferedReader input = null;
		Process p = null;
		try
		{
			String[] strI = new String[3];// {"ps","-ef"};//,"|","grep","CenterData.jar"};
			strI[0] = "/bin/sh";
			strI[1] = "-c";
			strI[2] = strInput;

			p = Runtime.getRuntime().exec(strI); // 调用Linux的相关命令
			ir = new InputStreamReader(p.getInputStream());
			input = new BufferedReader(ir);
			String line = "";
			if(strInput.indexOf("java -jar") != 0)
			{
				while(!java.util.Objects.equals((line = input.readLine()),null))
				{
					strResult += line.trim() + "\r\n";
					// 按行打印输出内容
				}
			}
		}
		catch(IOException e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		finally
		{
			try
			{
				ir.close();
			}
			catch(IOException e)
			{
			}
			try
			{
				input.close();
			}
			catch(IOException e)
			{
			}
			if(!java.util.Objects.equals(p,null))
				p.destroy();
		}
		return strResult;
	}

	// 调用linux系统命令
	public String useLinuxCommond(String strInput)
	{
		String strResult = "";
		InputStreamReader ir = null;
		// LineNumberReader input = null;
		BufferedReader input = null;
		try
		{
			Process p = Runtime.getRuntime().exec(strInput); // 调用Linux的相关命令
			ir = new InputStreamReader(p.getInputStream());
			// input = new LineNumberReader(ir); // 创建IO管道，准备输出命令执行后的显示内容
			input = new BufferedReader(ir);
			String line = "";
			if(strInput.indexOf("java -jar") != 0)
			{
				while(!java.util.Objects.equals((line = input.readLine()),null))
				{
					strResult += line;
					// 按行打印输出内容
				}
			}
		}
		catch(IOException e)
		{
			Com_Fun.debug_in(e,this.toString());
		}
		finally
		{
			try
			{
				input.close();
			}
			catch(IOException e1)
			{
			}
			try
			{
				ir.close();
			}
			catch(IOException e)
			{
			}
		}
		return strResult;
	}

	@SuppressWarnings("unchecked")
	public static void openURL(String url)
	{
		String osName = System.getProperty("os.name");
		try
		{
			if(osName.startsWith("Mac OS"))
			{
				// doc
				@SuppressWarnings("rawtypes")
				Class fileMgr = Class.forName("com.apple.eio.FileManager");
				Method openURL = fileMgr.getDeclaredMethod("openURL",new Class[]
				{ String.class });
				openURL.invoke(null,new Object[]
				{ url });
			}else if(osName.startsWith("Windows"))
			{
				// Windows
				Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler "
						+ url);
			}else
			{
				// assume Unix or Linux
				String[] browsers =
				{ "firefox", "opera", "konqueror", "epiphany", "mozilla",
						"netscape" };
				String browser = null;
				for(int count = 0;count < browsers.length
						&& java.util.Objects.equals(browser,null);count++)
				{
					if(Runtime.getRuntime().exec(new String[]
					{ "which", browsers[count] }).waitFor() == 0)
					{
						browser = browsers[count];
					}
				}
				if(!java.util.Objects.equals(browser,null))
				{
					Runtime.getRuntime().exec(new String[]
					{ browser, url });
				}
			}
		}
		catch(Exception ex)
		{
		}
	}

	public void RunFireFox()
	{
		try
		{
			Date dtNow = null;
			// mysql启动
			Auto_Command acd = new Auto_Command();
			File file = new File("/usr/lib64/firefox/firefox");
			if(file.exists())
			{
				strResult = acd.useLinuxCommond("/usr/lib64/firefox/firefox localhost:8080/icard");// firefox
			}
			dtNow = new Date();
			while(new Date().getTime() - dtNow.getTime() < 5000)
			{
				try
				{
					Thread.sleep(100);
				}
				catch(InterruptedException e)
				{
				}
			}
		}
		catch(Exception ssd)
		{
		}
	}
}